package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.dto.CheckGroupDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.mapper.CheckGroupMapper;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.vo.CheckGroupVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {


    @Override
    public boolean importOrderSettings(List<String[]> readExcelList) {
        List<OrderSetting> orderSettingList = changeStringToList(readExcelList);
        //    1    2020-12-15   100  0
        //  根据日期判断数据库表里面是否已经存在   如果已经存在  该记录主键 赋值 OrderSetting
        for (OrderSetting orderSetting : orderSettingList) {
            QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<OrderSetting>();
            orderSettingQueryWrapper.eq("ORDERDATE",orderSetting.getOrderDate());
            OrderSetting orderSettingExistOrNot = baseMapper.selectOne(orderSettingQueryWrapper);
            if(orderSettingExistOrNot!=null){
                //  数据库存在该日期记录  查询当前记录id  赋值 OrderSetting对象  mp会自动执行 update
                  orderSetting.setId(orderSettingExistOrNot.getId());
            }
        }
        boolean flag = saveOrUpdateBatch(orderSettingList,100);
        return flag;
    }

    @Override
    public Map findSettingData(int year, int month) {
       String  beginDate = year+"-"+month+"-1";
       String  endDate = year+"-"+month+"-31";
       List<OrderSetting> orderSettingList = baseMapper.findSettingDataByOrderDate(beginDate,endDate);

       Map<String,OrderSetting> map = new HashMap<String,OrderSetting>();
        for (OrderSetting orderSetting : orderSettingList) {
            OrderSetting   orderSetting1 = new OrderSetting();
            orderSetting1.setNumber(orderSetting.getNumber());
            orderSetting1.setReservations(orderSetting.getReservations());
            map.put(DateUtils.parseDate2String(orderSetting.getOrderDate(),"yyyy-MM-dd"),orderSetting1);
        }
        return map;
    }

    @Override
    public boolean updateNumberByOrderDate(int number, String orderdate) {
        baseMapper.updateNumberByOrderDate(number,orderdate);
        return false;
    }

    @Override
    public int isOrderOKorNot(String orderdate) {
        return baseMapper.isOrderOkorNot(orderdate);
    }

    @Override
    @LcnTransaction
    public void updateReservationsByOrderDate(String orderdate) {

        baseMapper.updateReservationsByOrderDate(orderdate);
    }

    private List<OrderSetting> changeStringToList(List<String[]> readExcelList) {
           if(readExcelList!=null&&readExcelList.size()!=0){
               List<OrderSetting> list = new ArrayList<OrderSetting>();
               for (String[] strings : readExcelList) {
                   OrderSetting orderSetting = new OrderSetting();
                   orderSetting.setReservations(0);
                   orderSetting.setNumber(Integer.parseInt(strings[1]));
                   orderSetting.setOrderDate(DateUtils.parseString2Date(strings[0],"yyyy/MM/dd"));
                   list.add(orderSetting);
               }
               return list;
           }
           return null;

    }

    @Override
    public void clearLastMonthMembers() {
        //月份变化触发
        Calendar instance = Calendar.getInstance();
        int month = instance.get(Calendar.MONTH) + 1;//12
        int year = instance.get(Calendar.YEAR);//2020
//        if (month == 1) {
//            //如果是一月份,清除的就是去年12月份的数据 ,年份-1,月份变为12
//            year = year - 1;
//            String strYear = String.valueOf(year);
//            month = 12;
//            String strMonth = String.valueOf(month);
//        } else {
//            month = month - 1;//11
//            String strMonth = String.valueOf(month);
//            String strYear = String.valueOf(year);
//        }
//        String beginDay = year + "-" + month + "-01";
//        String endDay = year + "-" + month + "-31";

        String beginDay = "2020" + "-" + "07" + "-12";//2020-11-01
        String endDay = "2020" + "-" + "07" + "-13";//2020-11-31

        baseMapper.clearLastMonthMembers(beginDay, endDay);
    }
}
